import { __private, _decorator, Component, Input, Node, Sprite, SpriteAtlas, UITransform } from 'cc';
import { GAME_EVENT_ENUM, GAME_STATE_ENUM, GMAE_BOARD_ENUM } from './Enum';
import { CHANGE_BOARD, CHECK_CLEAR } from './Event';
import { DataManager } from './DataManager';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('Block')
export class Block extends Component {
    id: number;
    // 坐标
    x: number;
    y: number;
    // 尺寸大小
    width: number;
    height: number;
    // 层级
    level: number;
    // 块类型
    private _boardType: GMAE_BOARD_ENUM;
    // 块是什么卡牌
    type: number;
    // 压着哪些block
    hignerIds: number[];
    // 被哪些block压着
    lowerIds: number[];

    @property(SpriteAtlas)
    spriteAtlas: SpriteAtlas = null;

    // 旧的历史数据
    old_boardType: GMAE_BOARD_ENUM;
    old_x: number;
    old_y: number;
    old_width: number;
    old_height: number;
    old_level: number;


    public get boardType() {
        return this._boardType
    }


    public set boardType(data: GMAE_BOARD_ENUM) {
        if (this.boardType !== data)
            this._boardType = data;
        // 触发更改面板事件
        CHANGE_BOARD.emit(GAME_EVENT_ENUM.CHANGE_BOARD, this)
    }

    // 初始化方法
    init(block: BlockType) {
        // 将block对象的属性合并到当前对象中
        Object.assign(this, block)
        // 将当前对象添加到全局数据中
        DataManager.instance.blocks.push(this)

        this.old_boardType = block.boardType as GMAE_BOARD_ENUM;
        this.old_x = block.x;
        this.old_y = block.y;
        this.old_width = block.width;
        this.old_height = block.height;
        this.old_level = block.level;

        // 渲染当前对象
        this.render()
    }

    // 块渲染更新
    render() {
        this.node.setPosition(this.x, this.y)
        this.node.getComponent(UITransform).width = this.width
        this.node.getComponent(UITransform).height = this.height
        this.node.getComponent(UITransform).priority = this.level
        this.node.getComponent(Sprite).spriteFrame = this.spriteAtlas.getSpriteFrames()[this.type]
        this.node.getChildByName('bg').active = !this.clickable()
    }

    // 可点击的状态的方法
    clickable(): boolean {
        switch (this.boardType) {
            case GMAE_BOARD_ENUM.LEVEL: {
                if (DataManager.instance.clickable) return true
                return this.hignerIds && this.hignerIds.length <= 0
            }
            case GMAE_BOARD_ENUM.LEVEL_EXTEND: {
                return true
            }
            case GMAE_BOARD_ENUM.RANDOM_RIGHT:
            case GMAE_BOARD_ENUM.RANDOM_LEFT: {
                return this.hignerIds && this.hignerIds.length <= 0
            }
            default:
                return false
        }

        return null
    }

    // 入槽
    toSlot() {
        // 入槽后处理首相受影响的下方元素
        this.lowerIds.forEach(id => {
            let block = DataManager.instance.blocks.find(item => item.id == id)
            let index = block.hignerIds.findIndex(item => item == this.id)
            if (index >= 0) {
            // 重新渲染一下
            console.log(block.hignerIds)
                // 移除下一层元素中的当前元素id
                block.hignerIds.splice(index, 1)
            }
            // 重新渲染一下
            console.log(block.hignerIds)
            block.render()
        })
        // block入槽
        if (DataManager.instance.records.findIndex(item => item.id === this.id) < 0) {
            DataManager.instance.records.push(this)
        }

        this.level = 0
        this.y = 0
        this.boardType = GMAE_BOARD_ENUM.SLOT

        let slot_all = DataManager.instance.blocks.filter(block => block.boardType === GMAE_BOARD_ENUM.SLOT)
        let slot_same = DataManager.instance.blocks.filter(block => block.boardType === GMAE_BOARD_ENUM.SLOT && block.type === this.type)
        let maxLevel = slot_all.reduce((pre, cur) => Math.max(pre, cur.level), 0)
        slot_same.forEach(block => {
            block.level = maxLevel + 1
            // block.y = block.height * (maxLevel + 1)
            block.render()
        })
        // 校验清除事件
        CHECK_CLEAR.emit(GAME_EVENT_ENUM.CHECK_CLEAR, this)
    }

    // 出槽
    toSlotCancel() {
        // todo
    }

    protected onLoad(): void {
        // 注册点击事件
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
    }

    onTouchStart(event: Event) {
        if (DataManager.instance.gameStatus !== GAME_STATE_ENUM.RUNNNING) return
        if (!this.clickable()) return
        // 点击音效

        // 点击动画特效

        this.toSlot()
    }

    onTouchEnd(event: Event) {

    }

    // 销毁时取消事件监听
    protected onDestroy(): void {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchEnd, this)
    }


}

